const { query } = require('./db');
const { addFavoriteCount, reduceFavoriteCount } = require('./article');

async function exitsFavorite(postId, userId) {
    const sql = 'select * from favorites WHERE post_id =? AND user_id =?';
    const result = await query(sql, [postId, userId]);
    if (result && result.length > 0)
        return true;
    return false;
}

async function addFavorite(postId, userId) {
    const sql = 'INSERT INTO favorites (post_id, user_id) VALUES (?,?)';
    const result = await query(sql, [postId, userId]);
    if (result.affectedRows > 0) {
        await addFavoriteCount(postId);
    }
    return result;
}

async function deleteFavorite(postId, userId) {
    const sql = 'DELETE FROM favorites WHERE post_id =? AND user_id =?';
    const result = await query(sql, [postId, userId]);
    if (result.affectedRows > 0) {
        await reduceFavoriteCount(postId);
    }
    return result;
}

module.exports = {
    exitsFavorite,
    addFavorite,
    deleteFavorite,
};


